﻿Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.IO

Public Class CLS_FILE_FTP
    <DllImport("advapi32.DLL", SetLastError:=True)> _
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _
        ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
        ByRef phToken As IntPtr) As Integer
    End Function

    Public Shared Sub CopiarEnRecursoRed(ByVal Usuario As String, ByVal Clave As String, ByVal Archivo_origen As String, ByVal Archivo_destino As String)
        Dim admin_token As IntPtr
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim wid_admin As WindowsIdentity = Nothing
        Dim wic As WindowsImpersonationContext = Nothing

        Dim MiDominio As String = ""
        Dim MiUsuario As String = ""

        If UBound(Usuario.Split("\")) = 1 Then
            MiUsuario = Usuario.Split("\")(1)
            MiDominio = Usuario.Split("\")(0)
        Else
            MiUsuario = Usuario
            MiDominio = Archivo_destino.Split("\")(2)
        End If

        If LogonUser(MiUsuario, MiDominio, Clave, 9, 0, admin_token) <> 0 Then
            wid_admin = New WindowsIdentity(admin_token)
            wic = wid_admin.Impersonate()
            File.Copy(Archivo_origen, Archivo_destino, True)
        Else
            Throw New Exception("Los datos de acceso a la carpeta compartida, no son válidos.-")
        End If
    End Sub
End Class